package ctn.dao.hibernate.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import ctn.dao.inteface.CTNUserDao;
import ctn.hibernate.po.CtnGrp;
import ctn.hibernate.po.CtnUser;

@Component
public class CTNUserDaoImpl extends AbstractCTNDAO implements CTNUserDao {

	@Override
	public CtnUser getUserByID(Long ID) {
		return getHibernateTemplate().get(CtnUser.class, ID);

	}

	@Override
	@SuppressWarnings("unchecked")
	public CtnUser getUserByName(String Name) {
		List<CtnUser> list = getHibernateTemplate().find(
				"From CtnUser  where name=?", Name);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	@SuppressWarnings("unchecked")
	public CtnUser getUserByEmail(String Email) {
		List<CtnUser> list = getHibernateTemplate().find(
				"From CtnUser  where email=?", Email);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public CtnUser updateUser(CtnUser user) {
		this.getHibernateTemplate().update(user);
		return user;
	}

	@Override
	public CtnUser saveUser(CtnUser user) {
		this.getHibernateTemplate().saveOrUpdate(user);
		return user;
	}

	@Override
	public void delUser(CtnUser user) {
		getHibernateTemplate().delete(user);

	}

	@SuppressWarnings("unchecked")
	@Override
	public Set<CtnUser> getUserByGrpID( Long grpID) {
	if(grpID==null)	
		return null;
	CtnGrp grp=getHibernateTemplate().get(CtnGrp.class, grpID);
	if(grp==null)
		return null;
	return grp.getCtnUsers();
	
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public int validateUserName(final String username) {
		final String hql = "select count(*) from CtnUser where name = :username";
		Number ct;
		ct = (Number) this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery(hql).setParameter("username", username).uniqueResult();
			}
		});
		return ct.intValue();
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public int validateUserEmail(final String email) {
		final String hql = "select count(*) from CtnUser where email = :email";
		Number ct;
		ct = (Number) this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				return session.createQuery(hql).setParameter("email", email).uniqueResult();
			}
		});
		return ct.intValue();
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<CtnUser> getUsers(CtnUser ctnUser) {
		return this.getHibernateTemplate().findByExample(ctnUser);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public boolean validatePwd(final String pwd,final Long id){
		return (Boolean)this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql="select count(t.id) from CtnUser t where t.password = :pwd and t.id = :id";
				int result = ((Long)session.createQuery(hql).setLong("id", id).setString("pwd", pwd).uniqueResult()).intValue();
				return result == 1;
			}
			
		});
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public void resetEmailOrName(final String name,final String email,final Long id){
		this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				CtnUser po = (CtnUser)session.get(CtnUser.class, id);
				if(!StringUtils.isEmpty(email)){
					po.setEmail(email);
				}
				if(!StringUtils.isEmpty(name)){
					po.setName(name);
				}
				session.update(po);
				return null;
			}
			
		});
	}

	@Override
	public List<CtnUser> getGrpUsers(Long l) {
		List<CtnUser> list=new ArrayList<CtnUser>();
		CtnGrp grp=getHibernateTemplate().load(CtnGrp.class, l);
		list.addAll(grp.getCtnUsers());
		return list;
	}

}
